import { View } from "../../../../mvc/core/View";
import { LayerManager } from "../../../manager/LayerManager";
import { WindowLayer } from "../../../manager/WindowLayer";
import { PopupManager } from "../../../manager/PopupManager";
import { ImageInfo } from "../../../../../com/gengine/resource/info/ImageInfo";
import { ItemData } from "../../../resource/info/ItemData";
import { LoaderManager } from "../../../../../com/gengine/resource/LoaderManager";
import { SWFInfo } from "../../../../../com/gengine/resource/info/SWFInfo";
import { Global } from "../../../../../com/gengine/global/Global";
import { CopyDrawRewardComp } from "./CopyDrawRewardComp";
import { DataEvent } from "../../../events/DataEvent";
import { EProp } from "../../../../../Message/Public/EProp";
import { GameProxy } from "../../../mvc/GameProxy";
import { SecTimer } from "../../../../../com/gengine/core/frame/SecTimer";
import { TimerType } from "../../../../../com/gengine/core/frame/TimerType";
type int = number;
//class CopyDrawRewardPanel
    
    export  class CopyDrawRewardPanel extends View
    {
        constructor(arg1: Boolean=false)
        {
            super();
            this._showAutoNext = arg1;
            this.layer = LayerManager.windowLayer;
            this.addEventListener(flash.events.MouseEvent.MOUSE_DOWN, this.onWindowDownHandler);
            return;
        }

        /* internal  */onWindowDownHandler(arg1: Object /* flash.events.MouseEvent */): void
        {
            if (typeof layer === "windowlayer") 
            {
                PopupManager.setTop(this);
            }
            else 
            {
                layer.setTop(this);
            }
            return;
        }

        /* internal  */onLoadedBg(arg1: ImageInfo): void
        {
            var loc1=new Object /* flash.display.Bitmap */(arg1.bitmapData);
            this.addChildAt(loc1, 0);
            return;
        }

        public setDrawItem(arg1: ItemData): void
        {
            this._drawItem = arg1;
            return;
        }

        public getDrawItem(): ItemData
        {
            return this._drawItem;
        }

        public /* override */ show(arg1: int=0, arg2: int=0): void
        {
            super.show(arg1, arg2);
            if (this._drawItem) 
            {
                this.showDrawPane();
            }
            this.startCountDown(60);
            return;
        }

        public /* override */ hide(): void
        {
            super.hide();
            if (this._drawPanel && this._drawPanel.parent) 
            {
                this._drawPanel.parent.removeChild(this._drawPanel);
            }
            this.stopCountDown();
            return;
        }

        public showBigBoxEffect(): void
        {
            if (this._drawItem) 
            {
                if (this._dropBigBox) 
                {
                    this.onLoadBigBox();
                }
                else 
                {
                    LoaderManager.instance.load("DrawBigBoxDropEffect.swf", this.onLoadBigBox);
                }
            }
            return;
        }

        /* internal  */onLoadBigBox(arg1: SWFInfo=null): void
        {
            if (arg1) 
            {
                this._dropBigBox = arg1.clip;
            }
            this._dropBigBox.x = Global.stage.stageWidth / 2 - 508;
            this._dropBigBox.y = Global.stage.stageHeight / 2 - 280;
            this.addChild(this._dropBigBox);
            this._dropBigBox.gotoAndPlay(1);
            this._dropBigBox.addEventListener(flash.events.Event.ENTER_FRAME, this.onBigBoxEnterFrame);
            return;
        }

        /* internal  */onBigBoxEnterFrame(arg1: Object /* flash.events.Event */): void
        {
            if (this._dropBigBox.currentFrame == this._dropBigBox.totalFrames) 
            {
                this._dropBigBox.stop();
                this._dropBigBox.removeEventListener(flash.events.Event.ENTER_FRAME, this.onBigBoxEnterFrame);
                this.showDrawPane();
            }
            return;
        }

        /* internal  */showDrawPane(): void
        {
            if (this._dropBigBox && this._dropBigBox.parent) 
            {
                this._dropBigBox.parent.removeChild(this._dropBigBox);
            }
            if (!this._drawPanel) 
            {
                this._drawPanel = new CopyDrawRewardComp(this._showAutoNext);
                this._drawPanel.x = Global.stage.stageWidth / 2 - 166;
                this._drawPanel.y = Global.stage.stageHeight / 2 - 166;
                this._drawPanel.addEventListener(CopyDrawRewardComp.DrawComplete, this.onDrawComplete);
                this._drawPanel.addEventListener(CopyDrawRewardComp.OPEN_COPYBOX_SUCCESS, this.onOpenBoxSuccess);
                this._drawPanel.addEventListener(CopyDrawRewardComp.CLOSE_COPYBOX, this.onCloseCopyBox);
            }
            this._drawPanel.drawItem = this._drawItem;
            this.addChild(this._drawPanel);
            this._drawPanel.playShowEffect();
            return;
        }

        /* internal  */onDrawComplete(arg1: DataEvent): void
        {
            this.startCountDown(2);
            if (this._drawItem && this._drawItem.type == EProp._EPropCopyBox) 
            {
                GameProxy.copy.broadcastOpenCopyBoxItem();
                this.dispatchEvent(new DataEvent(CopyDrawRewardComp.DrawComplete, this._drawItem));
            }
            else if (this._drawItem && this._drawItem.type == EProp._EPropHappyBag) 
            {
                GameProxy.happyBag.broadcastOpenHappyBagItem();
                this.dispatchEvent(new DataEvent(CopyDrawRewardComp.DrawComplete, arg1.data));
            }
            return;
        }

        /* internal  */onOpenBoxSuccess(arg1: Object /* flash.events.Event */): void
        {
            this.stopCountDown();
            return;
        }

        /* internal  */onCloseCopyBox(arg1: Object /* flash.events.Event */): void
        {
            this.hide();
            if (this._drawItem && this._drawItem.type == EProp._EPropCopyBox) 
            {
                GameProxy.copy.openCopyBox(this._drawItem.uid, false, null);
            }
            return;
        }

        public isDrawing(): Boolean
        {
            return this._drawPanel && this._drawPanel.isDrawing();
        }

        public stageResize(): void
        {
            if (this._drawPanel) 
            {
                this._drawPanel.x = Global.stage.stageWidth / 2 - 166;
                this._drawPanel.y = Global.stage.stageHeight / 2 - 166;
            }
            return;
        }

        /* internal  */startCountDown(arg1: int): void
        {
            if (this._timer) 
            {
                this._timer.stop();
                this._timer.dispose();
                this._timer = null;
            }
            this._timer = new SecTimer(1, arg1);
            this._timer.addListener(TimerType.ENTERFRAME, this.onCountDownComplete);
            this._timer.start();
            return;
        }

        /* internal  */onCountDownComplete(arg1: SecTimer): void
        {
            if (this._drawPanel) 
            {
                this._drawPanel.setCountDown(arg1.repeatCount);
            }
            if (arg1.repeatCount == 0) 
            {
                if (this._drawPanel && !this._drawPanel.hadDraw) 
                {
                    this._drawPanel.openDrawItem();
                }
                else 
                {
                    this.hide();
                }
            }
            return;
        }

        /* internal  */stopCountDown(): void
        {
            if (this._timer) 
            {
                this._timer.stop();
                this._timer.dispose();
                this._timer = null;
            }
            if (this._drawPanel) 
            {
                this._drawPanel.setCountDown(-1);
            }
            return;
        }

        /* internal */ /* var */_showAutoNext: Boolean;

        /* internal */ /* var */_drawItem: ItemData;

        /* internal */ /* var */_dropBigBox: Object /* flash.display.MovieClip */;

        /* internal */ /* var */_drawPanel: CopyDrawRewardComp;

        /* internal */ /* var */_timer: SecTimer;
    }
